<?php
/**
 * This is the model class for table "{{activity_log}}".
 *
 * The followings are the available columns in table '{{activity_log}}':
 * @property integer $id
 * @property integer $module_id
 * @property integer $module
 * @property integer $module_status_code
 * @property integer $scenario
 * @property string $value
 * @property string $create_timezone
 * @property string $create_timestamp
 * @property string $create_datetime
 * @property string $create_date
 * @property string $create_time
 * @property integer $create_by
 * @property string $update_timezone
 * @property string $update_timestamp
 * @property string $update_datetime
 * @property string $update_date
 * @property string $update_time
 * @property integer $update_by
 */
class ActivityLog extends ActiveRecord {

    public $module_;
    public $moduleId_;
    public $moduleStatus_;
    public $scenario_;
    public $attributes_;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return ActivityLog the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{activity_log}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('module_, moduleId_, moduleStatus_, scenario_, attributes_', 'required'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'module_' => 'Module',
            'moduleId_' => 'Module Id',
            'moduleStatus_' => 'Module Status',
            'scenario_' => 'Module Scenario',
            'attributes_' => 'Attributes',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('user_id', $this->user_id);
        $criteria->compare('ref_id', $this->ref_id);
        $criteria->compare('ref_status', $this->ref_status);
        $criteria->compare('module', $this->module);
        $criteria->compare('status', $this->status);
        $criteria->compare('value', $this->value, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    protected function beforeSave() {
        if (parent::beforeSave()) {

            if ($this->isNewRecord) {
                $this->module = strtolower($this->module_);
                $this->module_id = $this->moduleId_;
                $this->module_status_code = $this->moduleStatus_;
                $this->scenario = $this->scenario_;
                $this->value = CJSON::encode($this->attributes_);

                $this->setCreateDateTime();
                
            } else {
                $this->setUpdateDateTime();
            }
            return true;
        }
        else
            return false;
    }

    protected function afterSave() {
        parent::afterSave();
        
        $this->saveBrowserLog();
    }

}